﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class SpecialSkillDAL : ISpecialSkillDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SpecialSkillDAL()
        {
        }

        public IList<SpecialSkillInfo> ListSpecialSkill(SpecialSkillInfo pInfo)
        {
            string strSQL;
            int empSeq = pInfo.Employee == null ? 0 : pInfo.Employee.Sequence;

            List<SpecialSkillInfo> infoList = new List<SpecialSkillInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", pInfo.Sequence);
                dbManager.AddParameters(2, "@EmpSeq", empSeq);

                strSQL = "SELECT SKILL_SEQ, EMP_SEQ, SKILL_TYPE_SEQ, SKILL_DESCR, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM HRM_PS_EMP_SPECIAL_SKILLS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (pInfo.Sequence > 0 ? " AND SKILL_SEQ = @Seq" : "")
                    + (empSeq > 0 ? " AND EMP_SEQ = @EmpSeq" : "")
                    + " ORDER BY SKILL_SEQ";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeDAL empDAL = new EmployeeDAL();
                    EmployeeInfo empInfo = new EmployeeInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<EmployeeInfo> empList = empDAL.ListEmployees(new EmployeeInfo(rdr.GetInt32(1)));
                        empInfo = empList[0];
                    }

                    MasSpecialSkillTypeDAL typeDAL = new MasSpecialSkillTypeDAL();
                    MasSpecialSkillTypeInfo typeInfo = new MasSpecialSkillTypeInfo();
                    if (!rdr.IsDBNull(2))
                    {
                        IList<MasSpecialSkillTypeInfo> typeList = typeDAL.ListSpecialSkillTypes(new MasSpecialSkillTypeInfo(rdr.GetInt32(2)));
                        typeInfo = typeList[0];
                    }

                    SpecialSkillInfo Info = new SpecialSkillInfo(rdr.GetInt32(0)
                            , empInfo, typeInfo
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? null : (Nullable<int>)rdr.GetInt32(6)
                            , rdr.IsDBNull(7) ? null : (Nullable<int>)rdr.GetInt32(7));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch (Exception ex)
            {
                return null;
                throw ex;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public DataSet LoadSpecialSkill(SpecialSkillInfo pInfo)
        {
            string strSQL;
            int empSeq = pInfo.Employee == null ? 0 : pInfo.Employee.Sequence;

            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", pInfo.Sequence);
                dbManager.AddParameters(2, "@EmpSeq", empSeq);

                strSQL = "SELECT s.SKILL_SEQ, s.EMP_SEQ, s.SKILL_TYPE_SEQ, s.SKILL_DESCR, s.REMARKS"
                        + ", st.SKILL_TYPE_NAME, s.RECORD_STATUS, s.CREATED_BY, s.UPDATED_BY"
                    + " FROM HRM_PS_EMP_SPECIAL_SKILLS s"
                    + " LEFT JOIN MAS_SKILL_TYPES st ON s.SKILL_TYPE_SEQ = st.SKILL_TYPE_SEQ"
                    + " WHERE s.RECORD_STATUS = @ActiveRecord"
                    + (pInfo.Sequence > 0 ? " AND s.SKILL_SEQ = @Sequence" : "")
                    + (empSeq > 0 ? " AND s.EMP_SEQ = @EmpSeq" : "")
                    + " ORDER BY s.SKILL_SEQ";
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }

        public void Insert(SpecialSkillInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@Type", Info.Type == null ? (int?)null : Info.Type.Sequence);
                dbManager.AddParameters(2, "@Descr", Info.Description);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_EMP_SPECIAL_SKILLS"
                    + "(EMP_SEQ, SKILL_TYPE_SEQ, SKILL_DESCR, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@EmpSeq, @Type, @Descr, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(SpecialSkillInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@Type", Info.Type == null ? (int?)null : Info.Type.Sequence);
                dbManager.AddParameters(2, "@Descr", Info.Description);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@Sequence", Info.Sequence);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_SPECIAL_SKILLS"
                        + " SET SKILL_TYPE_SEQ = @Type"
                        + ", SKILL_DESCR = @Descr"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE SKILL_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_SPECIAL_SKILLS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE SKILL_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
